<template>
  <div class="login">
    <el-form :model="formData" status-icon :rules="rules" ref="form">
      <img style="width:90%;margin: auto;margin-bottom: 22px;" src="@/assets/school.png" />
      <el-form-item prop="username">
        <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" type="text" v-model="formData.username"
          autocomplete="off">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" type="password" v-model="formData.password"
          autocomplete="off" show-password></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
      </el-form-item>

      <a href="javascript:void(0)" @click="toRegister()">没有账号？去注册</a>
    </el-form>
  </div>
</template>

<script>
import storage from '@/utils/storage'
export default {
  name: 'Login',
  data() {
    // 校验
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入用户名'))
      }
      setTimeout(() => {
        if (value.length > 10 || value.length < 4) {
          callback(new Error('用户名长度为4~10位'))
        } else {
          callback()
        }
      }, 100)
    }
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入密码'))
      }
      setTimeout(() => {
        if (value.length > 16 || value.length < 6) {
          callback(new Error('密码长度为6~16位'))
        } else {
          callback()
        }
      }, 100)
    }
    return {
      schoolLogo:
        'https://www.cuz.edu.cn/images24/logo.png',
      formData: {
        username: 'admin',
        password: '123456',
      },
      role: 0,
      rules: {
        username: [{ validator: validateUsername, trigger: 'blur' }],
        password: [{ validator: validatePassword, trigger: 'blur' }],
      },
    }
  },
  created() { },
  methods: {
    submitForm() {
      this.$api.user.login(this.formData).then((res) => {
        if (res.code === 200) {
          storage.setToken(res.data)
          this.getUserInfo()
          this.$message({
            message: '登录成功',
            type: 'success',
          })

          setTimeout(() => {
            this.$router.push('/front/home')
          }, 100)
        } else {
          this.$message.error('登录失败')
        }
      })
    },
    toRegister() {
      setTimeout(() => {
        this.$router.push('/register')
      }, 200)
    },
    getUserInfo() {
      this.$api.user.getByUsername(this.formData.username).then((res) => {
        if (res.code === 200) {
          this.role = res.data.role
          storage.setLoginUser(res.data)
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;

  width: 100%;
  height: 100%;
  background-color: rgb(166, 189, 220);

  .el-form {
    display: flex;
    flex-direction: column;
    padding: 20px 20px 15px 20px;
    box-shadow: 2px 2px 20px 2px #726f6f;
    background-color: rgba(0, 0, 0, 0.6);
    border-radius: 18px;
    width: 300px;
    height: auto;
    margin: auto;


    .el-form-item {
      &:first-child {
        height: 71px;
      }

      img {
        width: 80%;
        margin: 0 10%;
      }

      .el-input {
        :deep(.el-input__inner) {
          background-color: rgba(0, 0, 0, 0.1);
          color: whitesmoke;
          border-radius: 16px;
          font-size: 14px;
        }
      }

      .el-button {
        width: 100%;
        font-size: 14px;
        background-color: rgba(0, 0, 0, 0.1);
        color: white;

        &:hover {
          color: pink;
          background-color: rgba(64, 158, 255, 0.2);
        }
      }
    }

    a {
      display: block;
      text-decoration: none;
      text-align: center;
      font-size: 12px;
      cursor: pointer;
      color: white;

      &:hover {
        color: pink;
      }
    }

    .el-form-item:nth-child(4) {
      margin-bottom: 10px;
    }
  }
}
</style>